package com.kerpow.KSheet;

import android.content.Context;
import android.graphics.Color;
import android.view.Display;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kerpow.KSheet.character.Power;

public class PowerView extends FrameLayout {
	
	private Context _context;
	private Power _power;
	private OnPowerClickedHandler _powerClickHandler;
	private TextView _title;
	private TextView _usedIndicator;
	private GestureDetector _gestureScanner;
	
	public interface OnPowerClickedHandler{
		public void OnPowerLongClick(PowerView power);
		public void OnPowerClick(PowerView power);
	}
	
	public PowerView(Context context, Power p, OnPowerClickedHandler handler){
		super(context);
		_powerClickHandler = handler;
		_context = context;
		_power = p;
		WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay(); 
        int screenWidth = (int)Math.round(display.getWidth());
        this.setLayoutParams(new LayoutParams(screenWidth, LayoutParams.FILL_PARENT));
        
		LayoutInflater inflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.powerview, null);	
		this.addView(ll);
		screenWidth = (int)Math.round(screenWidth);				
		
		OnLongClickListener onLongClick = new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				PowerView.this.performPowerLongClick();
				return false;
			}
		};
		 
		OnClickListener onClick = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				PowerView.this.performPowerClick();
			}
		};
		
		
		_usedIndicator = (TextView) findViewById(R.id.used);
		_title = (TextView) findViewById(R.id.title);
		//_title.setOnLongClickListener(showMenu);
		_title.setOnClickListener(onClick);
		_title.setOnLongClickListener(onLongClick);
		setTitle();
		
		TextView t = (TextView) findViewById(R.id.stats);
		t.setText(_power.getSmallDetail());
		//t.setOnLongClickListener(showMenu);
		t.setOnClickListener(onClick);
		t.setOnLongClickListener(onLongClick);
			
		String html = "<html><head><style>" +
			"body{color: white; background: black; margin:5px 20px 5px 20px; font-size: 10pt;}" +
			"h1{ display: none; }"+
			".level{display: none}" +
			"</style></head><body>";
		
		if(p.WebDetail.equals(""))
			html +=	"No compendium entry found. Use a book.</body></html>";
		else
			html +=	p.WebDetail + "</body></html>";

		WebView wv = (WebView) findViewById(R.id.html);
		
		wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		wv.loadData(html, "text/html", "utf-8");

		
		_gestureScanner = new GestureDetector(context, new PowerViewGestureListener());
		wv.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				_gestureScanner.onTouchEvent(event);
				return false;
			}
			
		});
		
		this.setOnClickListener(onClick);
		this.setOnLongClickListener(onLongClick);
		//this.setOnLongClickListener(showMenu);
	}
	
	public void markPowerUsed(){
		_power.Used = true;
		setTitle();
	}
	public void markPowerUnUsed(){
		_power.Used = false;
		setTitle();
	}
	private void performPowerLongClick(){
		if(_powerClickHandler != null)
			_powerClickHandler.OnPowerLongClick(this);
	}
	private void performPowerClick(){
		if(_powerClickHandler != null)
			_powerClickHandler.OnPowerClick(this);
	}

	private void setTitle(){

		_title.setText(_power.Name);
		if(_power.Used)
		{
			_title.setBackgroundColor(Color.BLACK);
			_title.setTextColor(Color.GRAY);
			_usedIndicator.setVisibility(View.VISIBLE);
		}
		else
		{
			int color = Color.BLACK;
			if(_power.Usage.contains("At-Will"))color = Color.argb(255, 0, 143, 0);
			if(_power.Usage.contains("Encounter"))color = Color.argb(255, 143, 0, 0);
			if(_power.Usage.contains("Daily")) color = Color.argb(255, 47, 47, 47);
			
			_title.setBackgroundColor(color);
			_title.setTextColor(Color.WHITE);
			_usedIndicator.setVisibility(View.GONE);
		}
		
	}
	public boolean isUsed(){
		return _power.Used;
	}
	public boolean isAtWill(){
		return _power.Usage.equals("At-Will");
	}
	public boolean isAugmentable(){
		return _power.WebDetail.contains("Augment");
	
	}
	
	public Long getPowerID(){
		return Long.parseLong(_power.ID);
	}
	public PowerView(Context context) {
		super(context);
	}
	
	private class PowerViewGestureListener extends SimpleOnGestureListener{

		@Override
		public void onLongPress(MotionEvent e) {
			PowerView.this.performPowerLongClick();
		}

		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			PowerView.this.performPowerClick();
			return true;
		}
	}
}
